package NewTmas.Listeners;

import NewTmas.FramesandPanels.JobGraphPanel;
import NewTmas.FramesandPanels.LogFileFrame;
import NewTmas.Graphs.JGraphGenerated;
import NewTmas.Popupmenus.TaskPopupMenu;
import com.mxgraph.model.mxICell;
import com.mxgraph.swing.mxGraphComponent;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: jhauser
 * Date: Jun 16, 2011
 * Time: 5:34:35 PM
 * To change this template use File | Settings | File Templates.
 */
public class OpsModeMouseAdaptor extends MouseAdapter
{
    private JobGraphPanel panel;
    private String task;
    JGraphGenerated jgraphObject;


    public OpsModeMouseAdaptor(JGraphGenerated jgraphObject, JobGraphPanel panel)
    {
        this.panel = panel;
        this.jgraphObject = jgraphObject;
    }




    public void mouseReleased(MouseEvent e)
    {
        Check(e) ;

    }

    public void mousePressed(MouseEvent e) {

        Check(e);
    }

    public void mouseClicked(MouseEvent e)
    {

             if(e.getClickCount()==2 && e.getSource() instanceof mxGraphComponent.mxGraphControl)
        {

                Object cell = jgraphObject.graphComponent.getCellAt(e.getX(), e.getY());

                Point pt = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), jgraphObject.graphComponent);
                if (jgraphObject.graph.getModel().isVertex(cell))
                {
                    mxICell mxCell = (mxICell) cell;
                    for (Map.Entry<String, String> entry : panel.display_name.entrySet())
                    {
                        if (entry.getValue().equals(jgraphObject.graph.getLabel(cell)))
                        {
                            task = entry.getKey();
                            break;
                        }
                    }

                }

//                if(panel.task_status.containsKey(task))
//                    {
                          new LogFileFrame(panel.job, task, panel.date,  panel.dtp) ;
                 //   }

              e.consume();
        }


    }

    private void Check(MouseEvent e) {
        if (e.isPopupTrigger() && e.getSource() instanceof mxGraphComponent.mxGraphControl)

        {

                Object cell = jgraphObject.graphComponent.getCellAt(e.getX(), e.getY());

                Point pt = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), jgraphObject.graphComponent);
                if (jgraphObject.graph.getModel().isVertex(cell))
                {
                    mxICell mxCell = (mxICell) cell;
                    for (Map.Entry<String, String> entry : panel.display_name.entrySet())
                    {
                        if (entry.getValue().equals(jgraphObject.graph.getLabel(cell)))
                        {
                            task = entry.getKey();
                            break;
                        }
                    }
                   if( panel.task_status.containsKey(task)){
                    TaskPopupMenu popupMenu = new TaskPopupMenu(panel, task);
                    popupMenu.show(jgraphObject.graphComponent, pt.x, pt.y);
                   }
                }

                e.consume();

        }


    }





}
